﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DAO;
using DTO;
using PTExpress.Models;

namespace PTExpress.Controllers
{
    [Authorize]
    public class QLDatVeController : AbstractController
    {

        public ActionResult Index()
        {
            DatVeDAO datVeDAO = new DatVeDAO();
            List<DatVeDTO> ls = datVeDAO.GetAll();
            this.ViewData.Add("dsDatVe", ls);
            return View();
        }

        public ActionResult DatVe()
        {
            GheDAO gheDAO = new GheDAO();
            List<GheDTO> ls = gheDAO.GetAll();
            ViewData["ds_ghe"] = ls;
            return View();
        }

        [HttpPost]
        public ActionResult DatVe(DatVe model)
        {
            GheDAO gheDAO = new GheDAO();
            List<GheDTO> ls = gheDAO.GetAll();
            ViewData["ds_ghe"] = ls;

            int a = model.SoGhe;

            if (ModelState.IsValid)
            {
                DatVeDAO datVeDAO = new DatVeDAO();
                DatVeDTO datVeDTO = new DatVeDTO();
                datVeDTO.KhachHangId = model.KhachHang;
                datVeDTO.ChuyenXeId = model.ChuyenXe;
                datVeDTO.NhanVienId = nhanVienDTO.Id;
                datVeDTO.Ghe = model.SoGhe;
                datVeDTO.NgayDatVe = DateTime.Now;

                int maLoi = datVeDAO.AddNew(datVeDTO);
                ViewData["ThanhCong"] = maLoi == 0;
                ViewData["maLoi"] = maLoi;
                if (maLoi == 0)
                {
                    return RedirectToAction("Index");
                }
            }
            return View(model);
        }

        public ActionResult ThanhToan(int id)
        {
            DatVeDAO dvDAO = new DatVeDAO();
            DatVeDTO dvDTO = dvDAO.getById(id);
            ChuyenXeDAO chuyenXeDAO = new ChuyenXeDAO();
            ChuyenXeDTO cx = chuyenXeDAO.getById(dvDTO.ChuyenXeId);
            DatVe model = new DatVe();
            model.Id = id;
            model.KhachHang = dvDTO.KhachHangId;
            model.ChuyenXe = dvDTO.ChuyenXeId;
            model.SoGhe = dvDTO.Ghe;
            model.GiaVe = cx.GiaVe;

            return View(model);
        }

        [HttpPost]
        public ActionResult ThanhToan(DatVe model, int id)
        {
            double giave = new double();
            double tongtien = new double();
            bool reset = new bool();
            double diemhientai = new double();
            DatVeDTO datVeDTO = new DatVeDTO();
            DatVeDAO datVeDAO = new DatVeDAO();
            datVeDTO.Id = id;
            bool thanhCong = datVeDAO.ThanhToan(datVeDTO, ref giave, ref tongtien, ref reset, ref diemhientai);

            if (thanhCong)
            {
                ViewData["giave"] = giave;
                ViewData["giave"] = giave;
                ViewData["reset"] = reset;
                ViewData["diemhientai"] = diemhientai;
                return RedirectToAction("Index", "QLDatVe");
            }
            else
            {
                return RedirectToAction("Error", "QLDatVe", new { mess = "Hệ thống đang bận, vui lòng thử lại sau." });
            }
        }

        public ActionResult Error(string mess)
        {
            ViewData["mess"] = mess;
            return View();
        }

        public ActionResult Huy(int id)
        {
            DatVeDAO dvDAO = new DatVeDAO();
            DatVeDTO dvDTO = dvDAO.getById(id);
            ChuyenXeDAO chuyenXeDAO = new ChuyenXeDAO();
            ChuyenXeDTO cx = chuyenXeDAO.getById(dvDTO.ChuyenXeId);
            DatVe model = new DatVe();
            model.Id = id;
            model.KhachHang = dvDTO.KhachHangId;
            model.ChuyenXe = dvDTO.ChuyenXeId;
            model.SoGhe = dvDTO.Ghe;
            model.GiaVe = cx.GiaVe;

            return View(model);
        }

        [HttpPost]
        public ActionResult Huy(DatVe model, int id)
        {
            DatVeDTO datVeDTO = new DatVeDTO();
            DatVeDAO datVeDAO = new DatVeDAO();
            datVeDTO.Id = id;
            datVeDTO.Ghe = model.SoGhe;
            bool thanhCong = datVeDAO.HuyDatVe(datVeDTO);

            if (thanhCong)
            {
                return RedirectToAction("Index", "QLDatVe");
            }
            else
            {
                return RedirectToAction("Error", "QLDatVe", new { mess = "Hệ thống đang bận, vui lòng thử lại sau." });
            }
        }
    }
}
